package com.dc.free.config;

import com.dc.free.controller.TestController;
import com.dc.free.handler.GlobalHandler;
import com.dc.free.module.lawyer.controller.LawyerController;
import com.dc.free.module.lawyer.db.model.MappingLawyer;
import com.dc.free.module.synchronization_common_peson.controller.SynchronizationCommonPesonController;
import com.dc.free.module.synchronization_common_peson.db.model._MappingKit;
import com.dc.free.module.synchronization_common_peson.service.SynchronizationCommonPesonScanner;
import com.dc.free.module.synchronization_zsedz_view.controller.SynchronizationZsedzViewController;
import com.dc.free.module.synchronization_zsedz_view.service.SynchronizationZsedzViewScanner;
import com.dc.free.util.DruidPluginUtil;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.Const;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

public class Config extends JFinalConfig{
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		loadPropertyFile("free.properties");
		me.setDevMode(true);
		me.setBaseDownloadPath(Const.DEFAULT_BASE_UPLOAD_PATH);
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		me.setBaseViewPath("view");
		me.add("test", TestController.class);
		me.add("scp", SynchronizationCommonPesonController.class);
		me.add("szv", SynchronizationZsedzViewController.class);
		me.add("lawyer", LawyerController.class);
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		//me.addSharedObject("shiro", new ShiroTag());
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		// 配置C3p0数据库连接池插件
		DruidPlugin dp = DruidPluginUtil.create();
        me.add(dp);	
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin("main", dp);
        arp.setDialect(DruidPluginUtil.getDialect());  //指定 Dialect
        //配置属性名(字段名)大小写不敏感容器工厂
        arp.setContainerFactory(new CaseInsensitiveContainerFactory());
        _MappingKit.mapping(arp);
        MappingLawyer.mapping(arp);
        com.dc.free.module.synchronization_zsedz_view.db.model._MappingKit.mapping(arp);
        
        me.add(arp);
        
        //舟山常口库
        if(getPropertyToBoolean("zsedz", false)) {
	        DruidPlugin dpZsedz = new DruidPlugin(getProperty("jdbc.zsedz.url"),
	        		getProperty("jdbc.zsedz.user"), getProperty("jdbc.zsedz.password"));
	        me.add(dpZsedz);	
			// 配置ActiveRecord插件
			ActiveRecordPlugin arpZsedz = new ActiveRecordPlugin("zsedz", dpZsedz);
	        arpZsedz.setDialect(new OracleDialect());  //指定 Dialect
	        //配置属性名(字段名)大小写不敏感容器工厂
	        arpZsedz.setContainerFactory(new CaseInsensitiveContainerFactory());
	        com.dc.free.module.bgxm.db.model._MappingKit.mapping(arpZsedz);
	        com.dc.free.module.hjdzxx.db.model._MappingKit.mapping(arpZsedz);
	        com.dc.free.module.hjsfzm.db.model._MappingKit.mapping(arpZsedz);
	        com.dc.free.module.lsrksfzm.db.model._MappingKit.mapping(arpZsedz);
	        com.dc.free.module.zxhkxx.db.model._MappingKit.mapping(arpZsedz);
	        me.add(arpZsedz);
        }
	}

	@Override
	public void configInterceptor(Interceptors me) {
		//启动Shiro拦截器
		// TODO Auto-generated method stub
		//me.add(new WeiXInConfigIntercept());
		//me.add(new WeiXinAuthInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		me.add(new GlobalHandler());
		//me.add(new JsCssHandler());
		//me.add(new HtmlHandler());
	}
	
	@Override
	public void afterJFinalStart() {
		// TODO Auto-generated method stub
		super.afterJFinalStart();
		if(getPropertyToBoolean("scan", false)) {
			System.out.println("数据库扫描开始启动>>>>>>>>>>>>>");
			SynchronizationCommonPesonScanner.start();
			SynchronizationZsedzViewScanner.start();
		}
	}

	@Override
	public void beforeJFinalStop() {
		// TODO Auto-generated method stub
		super.beforeJFinalStop();
		if(getPropertyToBoolean("scan", false)) {
			SynchronizationCommonPesonScanner.stop();
			SynchronizationZsedzViewScanner.stop();
		}
	}

	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 80, "/free", 5);
	}
}
